package driver.android.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;

import driver.android.R;
import driver.util.Event;

public class TaskSummaryListAdapter extends ArrayAdapter {

    private List mTaskList;
    private LayoutInflater mLayoutInflater;

    public TaskSummaryListAdapter(@NonNull Context context, int resource, List tasks) {
        super(context, resource);
        this.mLayoutInflater = LayoutInflater.from(context);
        this.mTaskList = tasks;
    }

    @Override
    public int getCount() {
        return mTaskList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Nullable
    @Override
    public Object getItem(int position) {
        return mTaskList.get(position);
    }

    @Override
    public int getPosition(@Nullable Object item) {
        return super.getPosition(item);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = this.mLayoutInflater.inflate(R.layout.f_task_summary_item, parent, false);
            holder = new ViewHolder();
            holder.taskStatus = (TextView) convertView.findViewById(R.id.summary_task_text);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Event task = (Event) mTaskList.get(position);
        holder.taskStatus.setText(task.getHeader());
        return convertView;
    }

    //static--只执行一次,而且先执行
    private static class ViewHolder {
        TextView taskStatus;
    }
}
